package com.site.action.master;

import java.util.List;

import javax.annotation.Resource;

import com.site.dto.master.PropertyBizBean;
import com.site.service.master.PropertyService;
import com.site.util.StringUtils;
import com.site.util.base.action.BackendBaseAction;
import com.site.util.base.service.BaseService;


/**
 * 编码管理
 * @author Lee 
 *
 */
public class PropertyAction extends BackendBaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private PropertyBizBean propertyBizBean;
	
	private PropertyService propertyService;
	
	private List<PropertyBizBean> propertyBizBeanList;
	//全部property的备注，用于后台property查询条件
	private List<String> allPropertyRemarkList;
	
	public List<String> getAllPropertyRemarkList() {
		return allPropertyRemarkList;
	}
	public void setAllPropertyRemarkList(List<String> allPropertyRemarkList) {
		this.allPropertyRemarkList = allPropertyRemarkList;
	}
	public List<PropertyBizBean> getPropertyBizBeanList() {
		return propertyBizBeanList;
	}
	public void setPropertyBizBeanList(List<PropertyBizBean> propertyBizBeanList) {
		this.propertyBizBeanList = propertyBizBeanList;
	}
	public PropertyService getPropertyService() {
		return propertyService;
	}
	@Resource
	public void setPropertyService(PropertyService propertyService) {
		this.propertyService = propertyService;
	}

	public PropertyBizBean getPropertyBizBean() {
		return propertyBizBean;
	}

	public void setPropertyBizBean(PropertyBizBean propertyBizBean) {
		this.propertyBizBean = propertyBizBean;
	}

	/**
	 * 获得所有属性
	 * @return
	 */
	public String getPropertyList(){
		if(queryPage==null)
			super.initPagination();
		propertyBizBeanList = propertyService.getAllProperty(propertyBizBean, queryPage);
		allPropertyRemarkList = propertyService.findAllProperty();
		return "getPropertyList";
	}
	/**
	 * 增加属性页面
	 * @return
	 */
	public String getPropertyAdd(){
		return "getPropertyAdd";
	}
	/**
	 * 增加属性
	 * @return
	 */
	public String propertyAdd(){
		if(checkProperty(1))
			return "getPropertyAdd";
		propertyBizBean.setCreatedUser(getCurrentUser().getUserName());
		propertyBizBean.setUpdatedUser(getCurrentUser().getUserName());
		propertyService.execute(propertyBizBean, BaseService.COMMAND_SAVE);
		super.setMessageID("SYS_MSG_00022");
		super.setBackurl(request.getContextPath() + "/property/propertyAction!getPropertyList.do");
		return "toMessage";
	}
	
	/**
	 * 编辑属性页面
	 * @return
	 */
	public String getPropertyEdit(){
		if(null != propertyBizBeanList && propertyBizBeanList.size() > 0){
			for (PropertyBizBean propertyBizBean : propertyBizBeanList) {
				if(null != propertyBizBean && propertyBizBean.getPropertyId() != null){
					this.propertyBizBean = propertyService.getPropertyById(propertyBizBean.getPropertyId());
					break;
				}
			}
			return "getPropertyEdit";
		} else {
			return getPropertyList();
		}
	}
	/**
	 * 编辑属性
	 * @return
	 */
	public String propertyEdit(){
		if(checkProperty(2))
			return "getPropertyEdit";
		propertyService.execute(propertyBizBean, BaseService.COMMAND_UPDATE);
		super.setMessageID("SYS_MSG_00022");
		super.setBackurl(request.getContextPath() + "/property/propertyAction!getPropertyList.do");
		return "toMessage";
	}
	
	/**
	 * 删除属性
	 * @return
	 */
	public String propertyDelete(){
		if(null !=propertyBizBeanList && propertyBizBeanList.size() > 0){
			for (PropertyBizBean propertyBizBean : propertyBizBeanList) {
				if(null != propertyBizBean && propertyBizBean.getPropertyId() != null){
					propertyService.tombstoneProperty(propertyBizBean.getPropertyId());
				}
			}
			super.setMessageID("SYS_MSG_00022");
		} else {
			super.setMessageID("SYS_MSG_00026");
		}
		super.setBackurl(request.getContextPath() + "/property/propertyAction!getPropertyList.do");
		return "toMessage";
	}
	/**
	 * 属性检查
	 * @param type
	 * @return
	 */
	private boolean checkProperty(int type){
		boolean isError = false;
		if(type==1){
			if(StringUtils.isEmpty(propertyBizBean.getPropertyKey())){
				super.addFieldError("propertyKey","属性名不能为空！");
				isError=true;
			} else {
				boolean isExists = propertyService.isExistsProertyKey(propertyBizBean.getPropertyKey().trim(), 0);
				if(isExists) {
					super.addFieldError("propertyKey","属性名称已存在！");
					isError=true;
				}
			}
		} else {
			
			if(StringUtils.isEmpty(propertyBizBean.getPropertyKey())){
				super.addFieldError("propertyKey","属性名不能为空！");
				isError=true;
			} else {
				boolean isExists = propertyService.isExistsProertyKey(propertyBizBean.getPropertyKey().trim(), propertyBizBean.getPropertyId());
				if(isExists){
					super.addFieldError("propertyKey","属性名称已存在！");
					isError=true;
				}
			}
		}
		if(StringUtils.isEmpty(propertyBizBean.getPropertyValue())){
			super.addFieldError("propertyValue","属性值不能为空！");
			isError=true;
		}
		if(StringUtils.isEmpty(propertyBizBean.getRemark())){
			super.addFieldError("remark","备注不能为空！");
			isError=true;
		}
		
		return isError;
	}
}
